<?php
include_once("../configure/configure.php");						//--> global var
include_once("../admin/configure/admin.config.inc.php");		//-- admin var
include_once("../admin/tools/smarty/libs/Smarty.class.php");	//--> out template

include_once("impl/ShareCouponImpl.php");
include_once("impl/PromotionTypeImpl.php");

include_once("../includeSmarty.php");
include_once("../includeTag.php");
include_once("../includeCategories.php");

include_once("checkLogin.php");// check login status
include_once("control.php");// user global action control

$shareImpl 		= new ShareCouponImpl(DB_TAG_COUPON);
$promoTypeImpl	= new PromotionTypeImpl(DB_TAG_COUPON);




if(isset($_POST['Submit'])){	
	$promoTypeId	= $_POST['promoTypeId'];
	$store 			= $_POST['store'];
	$code 			= $_POST['code'];
	$name 			= $_POST['name'];
	$descript 		= $_POST['descript'];
	$startDate 		= $_POST['startDate'];
	$endDate 		= $_POST['endDate'];
	
	$status 		= 'waitting';
	
	if(empty($name))
		$error_message = 'Name is required.';
	else if(empty($store))
		$error_message = 'Store is required.';
	else if(empty($endDate))
		$error_message = 'The end is required.';
	else{
		$obj = new ShareCouponDomain();
		
		$obj->setUserId($userId);
		$obj->setCouponId(0);
		$obj->setName($name);
		$obj->setCode($code);
		$obj->setStore($store);
		$obj->setPromoTypeId($promoTypeId);
		$obj->setDescript($descript);
		$obj->setStartDate($startDate);
		$obj->setEndDate($endDate);
		
		$obj->setStatus($status);
		
		
		$isReturn = $shareImpl->add($obj);
		if($isReturn){		
			$location = "admin.php";
			
			header("Location: $location");
			exit;			
		}else
			$error_message = 'System error, register fail.';
	}
}

$arrPromoType = $promoTypeImpl->getSelect();

$smarty->assign('oTags', $oTags);
$smarty->assign('oCatTree', $oCatTree);

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL_HTTP);
$smarty->assign('userId', $userId);

$smarty->assign('message', $error_message);
$smarty->assign('arrPromoType', $arrPromoType);

$smarty->assign('name', $name);
$smarty->assign('store', $store);
$smarty->assign('code', $code);
$smarty->assign('promoTypeId', $promoTypeId);
$smarty->assign('descript', $descript);
$smarty->assign('startDate', $startDate);
$smarty->assign('endDate', $endDate);

$smarty->display('addCoupon.html');
?>
